//
//  SettingSendFeedbackVC.swift
//  AIPhotoProcess
//
//  Created by aw031 on 3/12/2025.
//

import UIKit
import IQTextView
import SnapKit
import MobileCoreServices
import MessageUI

class SettingSendFeedbackVC: BaseViewController,MFMailComposeViewControllerDelegate  {

    @IBOutlet weak var textFieldBgView: UIView!
    @IBOutlet weak var textFieldSubBgView: UIView!
    @IBOutlet weak var textField: UITextField!

    
    @IBOutlet weak var textViewBgView: UIView!
    @IBOutlet weak var textViewSubBgView: UIView!
    
    @IBOutlet weak var submitBtn: UIButton!

    lazy var textView: IQTextView = {
        let tv = IQTextView()
        tv.backgroundColor = .clear
        tv.font = FontScale(14)
        tv.layer.cornerRadius = KScreenWValue(8)
        tv.placeholder = "Please enter"
        tv.placeholderTextColor = Color9294B2
        return tv
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setUpdataUI()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        addTopImageView()

        view.sendSubviewToBack(topBackImageView)
        // Do any additional setup after loading the view.
        addNavigationBar(.leftAndTitle) { [weak self] (btn,index)  in
            self?.navigationController?.popViewController(animated: true)
        }
        
        navigationViewBar.centerTitle = "Feedback"
        
    }

    func setUpdataUI() {
        
        
        self.textViewSubBgView.addSubview(self.textView)
        self.textView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(8)
        }
        
        textFieldBgView.layer.cornerRadius = KScreenWValue(8)
        textFieldSubBgView.layer.cornerRadius = KScreenWValue(8)
        textFieldBgView.setViewWithShadowColorBlackAlpha01()
        
        textField.layer.cornerRadius = KScreenWValue(8)
        textField.backgroundColor = .clear
        textField.font = FontScale(14)
        textField.attributedPlaceholder = NSAttributedString(
            string: "Your email",
            attributes: [.foregroundColor: Color9294B2]
        )

        
        textViewBgView.layer.cornerRadius = KScreenWValue(8)
        textViewBgView.setViewWithShadowColorBlackAlpha01()

        textViewSubBgView.layer.cornerRadius = KScreenWValue(8)
        
        
        let btnImage = UIImage.imageGrandientLayer(with: CGSizeMake(KScreenWValue(251), KScreenWValue(50)), directionType: .leftToRight, cornerRadius: KScreenWValue(25))
        submitBtn.setCustomBackgroundImage(btnImage)
        submitBtn.titleLabel?.font = FontBlackScale(20)
        submitBtn.setViewWithShadowColor(ColorA669C5.withAlphaComponent(0.3) , shadowRadius: 10)
        
        
    }

    @IBAction func clickSubmitUpInside(_ sender: Any) {
        
        if KStringIsEmpty(self.textView.text.removeStartAndEndSpaces()) {
            MBProgressHUD.showToast(message: "请描述反馈的内容")
            return
        }
        
        self.sendEmailAction(self.textView.text.removeStartAndEndSpaces())
    }
    func sendEmailAction(_ emailStr: String) {
        if emailStr.isEmpty {
            return
        }
        
        if MFMailComposeViewController.canSendMail() {
            let mailCompose = MFMailComposeViewController()
            mailCompose.mailComposeDelegate = self
            #warning("修改邮箱")
            mailCompose.setToRecipients(["15029802896@163.com"])
            mailCompose.setSubject("Feedback")
            mailCompose.setMessageBody(emailStr, isHTML: false)
            
            self.present(mailCompose, animated: true, completion: nil)
        } else {
            MBProgressHUD.showToast(message: "This device is not configured to send email.")
        }
    }

    // MFMailComposeViewControllerDelegate
    func mailComposeController(_ controller: MFMailComposeViewController,
                              didFinishWith result: MFMailComposeResult,
                              error: Error?) {
        // 通知用户与界面相关的错误
        switch result {
        case .cancelled:
            MBProgressHUD.showToast(message:"Send cancel")
        case .saved:
            MBProgressHUD.showToast(message:"Send save")
        case .sent:
            MBProgressHUD.showToast(message:"Send success")
        case .failed:
            MBProgressHUD.showToast(message:"Send failed")
        @unknown default:
            break
        }
        
        controller.dismiss(animated: true, completion: nil)
    }
    
}

